﻿package {
	import com.janisRadins.asDraw.DrawUtils;
	import com.janisRadins.asDraw.Path;
	import flash.display.GraphicsPath;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import mx.utils.ObjectUtil;
	/**
	 * @author Jānis Radiņš
	 */
	public class T extends Sprite {
		
		private static var instance:T = null;
		public static function drawPoint(p:Point, col:Number = 0x123456):void {
			instance._drawPoint(p, col);
		}
		
		public static function drawCurve(s:Point, c:Point, a:Point):void {
			instance._drawCurve(s, c, a);
		}
		
		private var xmlData:XML = null;
		private var xmlPath:XML = null;
		private var loader:URLLoader = null;
		
		public static var pointSpr:Sprite = null;
		public static var lineSprite:Sprite = null;
		
		public function T() {
			instance = this;
			pointSpr = new Sprite();
			//pointSpr.y = 100;
			addChild(pointSpr);
			
			lineSprite = new Sprite();
			//lineSprite.y = 100;
			addChild(lineSprite);
			
			//loader = new URLLoader(new URLRequest("SVGPath.svg?T=" + (new Date().getTime())));
			loader = new URLLoader(new URLRequest("SVGPath1.svg?T=" + (new Date().getTime())));
			loader.addEventListener(Event.COMPLETE, onXMLLoaded);
		}
		
		private function onXMLLoaded(e:Event):void {
			xmlData = XML(loader.data);
			var sngNs:Namespace = new Namespace("http://www.w3.org/2000/svg");
			xmlPath = xmlData..sngNs::path[0];
			var path:Path = DrawUtils.svgToPath(xmlPath);
			
			var commands:GraphicsPath = path.getCurveSegment();
			var drawSpr:Sprite = new Sprite();
			drawSpr.graphics.lineStyle(2, 0xFF0000, 0.7);
			drawSpr.graphics.beginFill(0x123456, 0.5);
			drawSpr.graphics.drawPath(commands.commands, commands.data, commands.winding);
			drawSpr.graphics.endFill();
			addChild(drawSpr);
			
			var line:Sprite = new Sprite();
			line.graphics.beginFill(0xFF0000);
			line.graphics.drawCircle( -100, 0, 5);
			line.graphics.endFill();
			line.graphics.beginFill(0x00FF00);
			line.graphics.drawCircle(100, 0, 5);
			line.graphics.endFill();
			line.graphics.lineStyle(2, 0x123456);
			line.graphics.moveTo(-100, 0);
			line.graphics.lineTo(100, 0);
			addChild(line);
			
			var linePos:Number = 0;
			addEventListener(Event.ENTER_FRAME, function(e:Event):void {
				linePos += 10;
				linePos %= path.length;
				var p:Point = path.getPointOnCurve(linePos);
				line.x = p.x;
				line.y = p.y;
				line.rotation = path.getAngleAtPoint(linePos) * (180 / Math.PI);
			});
		}
		
		private function _drawPoint(p:Point, col:Number):void {
			pointSpr.graphics.beginFill(col, 0.6);
			pointSpr.graphics.drawCircle(p.x, p.y, 3);
			pointSpr.graphics.endFill();
		}
		
		private function _drawCurve(s:Point, c:Point, a:Point):void {
			pointSpr.graphics.lineStyle(1, 0x00FF00, 0.7);
			pointSpr.graphics.moveTo(s.x, s.y);
			pointSpr.graphics.curveTo(c.x, c.y, a.x, a.y);
		}
	}
}